package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;


import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.DateRegUtil;
import net.tycmc.cty.ctyms.timejob.common.DynamicReadConfig;
import net.tycmc.cty.ctyms.timejob.common.ListUtil;
import net.tycmc.cty.ctyms.timejob.common.NumberFormat;
import net.tycmc.cty.ctyms.timejob.iems.dao.EffectivenessStatDAO;

public class EffectivenessStatService {
	private static Logger logdata = Logger.getLogger("effectLog");
	private EffectivenessStatDAO effectivenessStatDAO;
	public void setEffectivenessStatDAO(EffectivenessStatDAO effectivenessStatDAO) {
		this.effectivenessStatDAO = effectivenessStatDAO;
	}
	/**
	 * 时效统计
	 **/
	public boolean startWork(boolean isflag) {
		Date date1 = new Date();
		boolean result=false;//统计结果
		System.out.println("开始执行时效统计：" + date1.toString());
		List<String> sqlList = new ArrayList<String>();
		List<Object[]> paramList = new ArrayList<Object[]>();
		// 从进出围栏提示表中获取提示时间为昨天，设备为JPP设备的所有数据，按照设备ID、时间均正序排序
		List<Map<String, Object>> listMap = this.effectivenessStatDAO.queryFenTipInfo();
		if(isflag){//是否手动执行ture为是
			this.effectivenessStatDAO.deleteEffect();
		}
		// 每个设备对应的List列表
		List<Map<String, Object>> listdataMap = new ArrayList<Map<String, Object>>();
		// 设备ID
		
		//二期优化：是否需要备份数据
		String isbakTable="";
		try {
			isbakTable = DynamicReadConfig.getConfigProperty("config.properties", "EffectivenessStat");
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!StringUtil.isValid(isbakTable)){
			isbakTable = "0";
		}
		
		String vehicleID = "";
		String isOpen = null;
		// 源数据为空，结束统计;不为空，继续
		if (listMap != null && listMap.size()>0) {
			// 按照指定key，将listMap封装成 MAP<设备ID，List<Map<String,Object>>>
			Map<String, List<Map<String, Object>>> dataMap = new ListUtil()
					.groupBy("MsgAF_Vcl_ID", listMap);
			// 循环,取每个设备对应的LIST在循环
			for (Map.Entry<String, List<Map<String, Object>>> entry : dataMap
					.entrySet()) {
				// 获得每个设备的List
				listdataMap = entry.getValue();
				//jinjiayu20141105修改提示开关
				isOpen = MapGetter.getString(listdataMap.get(0),"VclPS_isOpen");
				if("0".equals(isOpen)){
					continue;
				}
				// 获得设备ID
				vehicleID = entry.getKey();
				Map<String, Object> vclMapInfo = this.effectivenessStatDAO
						.queryInfoByVehicleId(vehicleID);
				// 获取此设备配置的里程
				String vehicleRumId = MapGetter.getString(vclMapInfo,
						"VehicleRumKmID");
				if (listdataMap.size() > 0) {
					// 判断第一条围栏类型
					// 围栏提示ID
					String fenTipId = MapGetter.getString(listdataMap.get(0),
							"MsgAF_ID");
					// 为进围栏
					if ("0".equals(MapGetter.getString(listdataMap.get(0),
							"MsgAF_AlarmType"))) {
						// 查询该设备是否有“未处理围栏”信息
						Map<String, Object> vclMap = this.effectivenessStatDAO
								.queryWaitDealInfoByVehicleId(vehicleID);
						// 存在，则配对为一组
						if (vclMap != null && vclMap.size() > 0) {
							// 根据提示信息ID获取进出围栏提示信息(出围栏)
							Map<String, Object> fenMapOut = this.effectivenessStatDAO
									.queryFenTipInfoById(MapGetter.getString(
											vclMap, "WDPassFT_PassFT_ID"),
											vehicleID,MapGetter.getString(
													vclMap, "WDPassFT_msgTime"));
							//判断，如果进出围栏为同一个，则忽略
							if(!MapGetter.getString(fenMapOut, "MsgAF_FenceID").equals(MapGetter.getString(listdataMap.get(0), "MsgAF_FenceID"))){
							// 获取此设备的运营线路
							String operLineName = MapGetter.getString(
									vclMapInfo, "operLineName");
							// 获取此设备本组对应线路规定时间、开始和结束序号
							Map<String, Object> mapData = 
							 this.getEffSTime(
									listdataMap.get(0), fenMapOut, vehicleID);
							//线路规定时间
							Double timeAll = NumberFormat.round(NumberFormat.toDouble(MapGetter.getString(mapData, "timeAll").toString()), 2);
							//开始围栏对应的序号
							int benginNum = Integer.parseInt(MapGetter.getString(mapData, "beEffOLNum").toString());
							//结束围栏对应的序号
							int endNum = Integer.parseInt(MapGetter.getString(mapData, "edEffOLNum").toString());
							// 发动机运行时间
							float ecmHour = this.getEcmHour(listdataMap.get(0),
									fenMapOut, vehicleID);
							// 平均速度
							Double averageSpeed = this.getAverageSpeed(
									listdataMap.get(0), fenMapOut,
									vehicleRumId, ecmHour);
							// 平均油耗
							Double averageOil = this.getMsgLSAvgOil(
									listdataMap.get(0), fenMapOut,
									vehicleRumId);
							// 平均载重
							Double averageAvgLoad = this.getMsgLSAvgLoad(
									listdataMap.get(0), fenMapOut, vehicleID);

							sqlList.add("insert into EffState(EffS_VclID,EffS_StateTime,EffS_IntoTime,EffS_OutTime,EffS_IntoFenceID,EffS_OutFenceID,EffS_OperName,EffS_Time,"
									+ "EffS_EgnTime,EffS_AvgSpeed,EffS_AvgLoad,EffS_AvgOil,EffS_IntoFenceNum,EffS_OutFenceNum) values (?,GETDATE(),?,?,?,?,?,?,?,?,?,?,?,?)");
							paramList.add(new Object[] {
									vehicleID,
									MapGetter.getString(fenMapOut,
											"MsgAF_AlarmTime"),
									MapGetter.getString(listdataMap.get(0),
											"MsgAF_AlarmTime"),
									MapGetter.getString(fenMapOut,
											"MsgAF_FenceID"),
									MapGetter.getString(listdataMap.get(0),
											"MsgAF_FenceID"), operLineName,
									timeAll, ecmHour, averageSpeed,
									averageAvgLoad,averageOil,benginNum,endNum });
							// 配对后删除“未处理围栏”中此条信息
							this.effectivenessStatDAO
									.deleteWaitDealPassFTById(MapGetter.getString(
											vclMap, "WDPassFT_PassFT_ID"),isbakTable);
						  } 
						}
						// 判断最后一条,如果最后一条为出围栏，保存
							if ("1".equals(MapGetter.getString(
									listdataMap.get(listdataMap.size() - 1),
									"MsgAF_AlarmType"))) {
								// 保存至该设备的“未处理围栏”中，此处保存进出围栏ID即可，根据ID关联其他信息
								this.effectivenessStatDAO.InsertWaitDealPassFT(
										vehicleID, MapGetter.getString(
												listdataMap.get(listdataMap
														.size() - 1),
												"MsgAF_ID"),MapGetter.getString(
														listdataMap.get(listdataMap
																.size() - 1),
														"MsgAF_AlarmTime"),isbakTable);
							}
							// 循环找寻下一条为出围栏的一条，开始配对
							for (int i = 0; i < listdataMap.size(); i++) {
								// 只有一条
								if (listdataMap.size() == 1) {
									continue;
								}
								// 多条,本条为出围栏从本条开始
								if ("1".equals(MapGetter.getString(
										listdataMap.get(i), "MsgAF_AlarmType"))) {
									// 判断下一条是否为进位栏，如果为进位栏则配对；若为出围栏，则下一条替换本条，继续寻找下一次进位栏
									if (i + 1 < listdataMap.size()) {
										// 有下一条,且下一条为进围栏，则配对
										if ("0".equals(MapGetter.getString(
												listdataMap.get(i + 1),
												"MsgAF_AlarmType"))) {
											// 配对
											Map<String, Object> fenMapOut = listdataMap
													.get(i);// 出围栏
											Map<String, Object> fenMapInt = listdataMap
													.get(i + 1);// 进围栏
											//判断，如果进出围栏为同一个围栏，则忽略
											if(!MapGetter.getString(fenMapOut, "MsgAF_FenceID").equals(MapGetter.getString(fenMapInt, "MsgAF_FenceID"))){
											
											// 获取此设备的运营线路
											String operLineName = MapGetter
													.getString(vclMapInfo,
															"operLineName");
											// 获取此设备本组对应线路规定时间
											Map<String, Object> mapData = 
													 this.getEffSTime(
															 fenMapInt, fenMapOut, vehicleID);
											//线路规定时间
											Double timeAll = NumberFormat.round(NumberFormat.toDouble(MapGetter.getString(mapData, "timeAll").toString()), 2);
											//开始围栏对应的序号
											int benginNum = Integer.parseInt(MapGetter.getString(mapData, "beEffOLNum").toString());
											//结束围栏对应的序号
											int endNum = Integer.parseInt(MapGetter.getString(mapData, "edEffOLNum").toString());
											// 发动机运行时间
											float ecmHour = this.getEcmHour(
													fenMapInt, fenMapOut,
													vehicleID);
											// 平均速度
											Double averageSpeed = this
													.getAverageSpeed(fenMapInt,
															fenMapOut,
															vehicleRumId,
															ecmHour);
											// 平均油耗
											Double averageOil = this.getMsgLSAvgOil(
													fenMapInt, fenMapOut,
													vehicleRumId);
											// 平均载重
											Double averageAvgLoad = this
													.getMsgLSAvgLoad(fenMapInt,
															fenMapOut,
															vehicleID);

											sqlList.add("insert into EffState(EffS_VclID,EffS_StateTime,EffS_IntoTime,EffS_OutTime,EffS_IntoFenceID,EffS_OutFenceID,EffS_OperName,EffS_Time,"
													+ "EffS_EgnTime,EffS_AvgSpeed,EffS_AvgLoad,EffS_AvgOil,EffS_IntoFenceNum,EffS_OutFenceNum) values (?,GETDATE(),?,?,?,?,?,?,?,?,?,?,?,?)");
											paramList.add(new Object[] {
													vehicleID,
													MapGetter.getString(
															fenMapOut,
															"MsgAF_AlarmTime"),
													MapGetter.getString(
															fenMapInt,
															"MsgAF_AlarmTime"),
													MapGetter.getString(
															fenMapOut,
															"MsgAF_FenceID"),
													MapGetter.getString(
															fenMapInt,
															"MsgAF_FenceID"),
													operLineName, timeAll,
													ecmHour, averageSpeed,
													averageAvgLoad,averageOil,benginNum,endNum });
											// 配对完成后，从i+2条开始继续循环找
											i = i + 1;
											// 下一条为出围栏，替换上一次出围栏
										   }
										} else {
											// 替换上一条出围栏
											continue;
											// 替换后，从i+1条开始继续循环找
										}

									}
								} else {
									continue;
								}
							}
						
						// 第一条为出围栏
					} else {
						// 判断最后一条,如果最后一条为出围栏，保存
						if ("1".equals(MapGetter.getString(
								listdataMap.get(listdataMap.size() - 1),
								"MsgAF_AlarmType"))) {
							// 保存至该设备的“未处理围栏”中，此处保存进出围栏ID即可，根据ID关联其他信息
							this.effectivenessStatDAO.InsertWaitDealPassFT(
									vehicleID, MapGetter.getString(
											listdataMap.get(listdataMap
													.size() - 1),
											"MsgAF_ID"),MapGetter.getString(
													listdataMap.get(listdataMap
															.size() - 1),
													"MsgAF_AlarmTime"),isbakTable);
						}
						for (int i = 0; i < listdataMap.size(); i++) {
							// 只有一条
							if (listdataMap.size() == 1) {
								continue;
							}
							// 多条,本条为出围栏从本条开始
							if ("1".equals(MapGetter.getString(
									listdataMap.get(i), "MsgAF_AlarmType"))) {
								// 判断下一条是否为进位栏，如果为进位栏则配对；若为出围栏，则下一条替换本条，继续寻找下一次进位栏
								if (i + 1 < listdataMap.size()) {
									// 有下一条,且下一条为进围栏，则配对
									if ("0".equals(MapGetter.getString(
											listdataMap.get(i + 1),
											"MsgAF_AlarmType"))) {
										// 配对
										Map<String, Object> fenMapOut = listdataMap
												.get(i);// 出围栏
										Map<String, Object> fenMapInt = listdataMap
												.get(i + 1);// 进围栏
										//判断，如果进出围栏为同一个围栏，则忽略
										if(!MapGetter.getString(fenMapOut, "MsgAF_FenceID").equals(MapGetter.getString(fenMapInt, "MsgAF_FenceID"))){
										
										// 获取此设备的运营线路
										String operLineName = MapGetter
												.getString(vclMapInfo,
														"operLineName");
										// 获取此设备本组对应线路规定时间
										Map<String, Object> mapData = 
												 this.getEffSTime(
														 fenMapInt, fenMapOut, vehicleID);
										//线路规定时间
										Double timeAll = NumberFormat.round(NumberFormat.toDouble(MapGetter.getString(mapData, "timeAll").toString()), 2);
										//开始围栏对应的序号
										int benginNum = Integer.parseInt(MapGetter.getString(mapData, "beEffOLNum").toString());
										//结束围栏对应的序号
										int endNum = Integer.parseInt(MapGetter.getString(mapData, "edEffOLNum").toString());
										// 发动机运行时间
										float ecmHour = this
												.getEcmHour(fenMapInt,
														fenMapOut, vehicleID);
										// 平均速度
										Double averageSpeed = this
												.getAverageSpeed(fenMapInt,
														fenMapOut,
														vehicleRumId, ecmHour);
										// 平均油耗
										Double averageOil = this.getMsgLSAvgOil(
												fenMapInt, fenMapOut,
												vehicleRumId);
										// 平均载重
										Double averageAvgLoad = this
												.getMsgLSAvgLoad(fenMapInt,
														fenMapOut, vehicleID);

										sqlList.add("insert into EffState(EffS_VclID,EffS_StateTime,EffS_IntoTime,EffS_OutTime,EffS_IntoFenceID,EffS_OutFenceID,EffS_OperName,EffS_Time,"
												+ "EffS_EgnTime,EffS_AvgSpeed,EffS_AvgLoad,EffS_AvgOil,EffS_IntoFenceNum,EffS_OutFenceNum) values (?,GETDATE(),?,?,?,?,?,?,?,?,?,?,?,?)");
										paramList.add(new Object[] {
												vehicleID,
												MapGetter.getString(fenMapOut,
														"MsgAF_AlarmTime"),
												MapGetter.getString(
														fenMapInt,
														"MsgAF_AlarmTime"),
												MapGetter.getString(fenMapOut,
														"MsgAF_FenceID"),
												MapGetter.getString(
														fenMapInt,
														"MsgAF_FenceID"),
												operLineName, timeAll, ecmHour,
												averageSpeed, averageAvgLoad,averageOil,benginNum,endNum });
										logdata.info("insert into EffState(EffS_VclID,EffS_StateTime,EffS_IntoTime,EffS_OutTime,EffS_IntoFenceID,EffS_OutFenceID,EffS_OperName,EffS_Time," +
												"EffS_EgnTime,EffS_AvgSpeed,EffS_AvgLoad,EffS_AvgOil,EffS_IntoFenceNum,EffS_OutFenceNum) values ("+vehicleID+",GETDATE(),'"+MapGetter.getString(fenMapOut,
														"MsgAF_AlarmTime")+"','"+MapGetter.getString(
																fenMapInt,"MsgAF_AlarmTime")+"','"+MapGetter.getString(fenMapOut,
																		"MsgAF_FenceID")+"','"+MapGetter.getString(
																				fenMapInt,"MsgAF_FenceID")+"','"+operLineName+"','"+timeAll+"','"+ecmHour+"','"+averageSpeed+"','"+averageAvgLoad+"','"+averageOil+"','"+benginNum+"','"+endNum+"')");
										// 配对完成后，从i+2条开始继续循环找
										i = i + 1;
										// 下一条为出围栏，替换上一次出围栏
										}
									} else {
										// 替换上一条出围栏
										continue;
										// 替换后，从i+1条开始继续循环找
									}

								}
							} else {
								continue;
							}
						}

					}

				}

			}
			
		//批量将数据插入统计表
			try {
				result = this.effectivenessStatDAO.insertAllToTable(sqlList, paramList,isbakTable);
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println("执行时效统计批量数据插入时发生错误！");
			}
			Date date2 = new Date();
			System.out.println("执行时效统计结束：" + date2.toString());
		}
		return result;
	}
	
	/**
	 * 获取规定时间
	 * @param fenMapInt
	 * @param fenMapOut
	 */
	public Map<String,Object> getEffSTime(Map<String,Object> fenMapInt,Map<String,Object> fenMapOut,String VehicleID){
		Map<String,Object> mapData = new HashMap<String, Object>();
		double timeAll = 0.0;
		//开始围栏ID
		String beFenId = MapGetter.getString(fenMapOut, "MsgAF_FenceID");
		//结束围栏ID
		String edFenId = MapGetter.getString(fenMapInt, "MsgAF_FenceID");
		//获取开始围栏ID对应的序号
		int beEffOLNum = this.effectivenessStatDAO.queryEffNumByFenId(beFenId,VehicleID);
		//获取结束围栏ID对应的序号
		int edEffOLNum = this.effectivenessStatDAO.queryEffNumByFenId(edFenId,VehicleID);
		mapData.put("beEffOLNum", beEffOLNum);
		mapData.put("edEffOLNum", edEffOLNum);
		//结束围栏前必须有围栏
		//判断开始和结束围栏的序号，序号值大的减一（不判断，车辆按围栏返回时，可能算不出时间）
		int signNumMax = 0;
		int signNumMin = 0;
		if(beEffOLNum>edEffOLNum){
			signNumMax = beEffOLNum;
			signNumMin = edEffOLNum;
		} else {
			signNumMax = edEffOLNum;
			signNumMin = beEffOLNum;
		}
		if(signNumMax-1>=0){
			signNumMax = signNumMax-1;
			 //获取规定时间
			 Double effSTimeAll =  this.effectivenessStatDAO.getEffTimeTotal(signNumMin, signNumMax, VehicleID);
			 timeAll = effSTimeAll;
		}
		mapData.put("timeAll", timeAll);
		return mapData;
		
	}
	/**
	 *获取两个围栏间发动机运行小时数
	 * @param fenMapInt
	 * @param fenMapOut
	 * @param VehicleID
	 * @return
	 */
	public float getEcmHour(Map<String,Object> fenMapInt,Map<String,Object> fenMapOut,String VehicleID){
		 //开始围栏对应
		String beEcmTime =  MapGetter.getString(fenMapOut, "MsgAF_tWorkTime_UDS");
		float beEcmTimeF = 0;
		if(beEcmTime.length()>0){
			beEcmTimeF = Float.parseFloat(beEcmTime);
		}
		//结束围栏对应
		String edEcmTime =  MapGetter.getString(fenMapInt, "MsgAF_tWorkTime_UDS");
		float edEcmTimeF = 0;
		if(edEcmTime.length()>0){
			edEcmTimeF = Float.parseFloat(edEcmTime);
		}
		return edEcmTimeF-beEcmTimeF;
		
	}
	/**
	 * 获取两个围栏间的平均速度
	 * @param fenMapInt
	 * @param fenMapOut
	 * @param vehicleRumId
	 * @param ecmHour
	 * @return
	 */
	public double getAverageSpeed(Map<String,Object> fenMapInt,Map<String,Object> fenMapOut,String vehicleRumId,float ecmHour){
		String vclRumKmOut = "";
		
		String vclRumKmInt = "";
		double averageSpeed = 0.0;
		//仪表
		if("1".equals(vehicleRumId)){
			vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKmExact");
			vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKmExact");
		//ECM高精度
		} else if("2".equals(vehicleRumId)){
			vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKmExact_ECM");
			vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKmExact_ECM");
		//ECM总里程
		}else if("3".equals(vehicleRumId)){
			vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKm_ECM");
			vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKm_ECM");
		//ECU里程
		}else if("4".equals(vehicleRumId)){
			vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKm_ECU");
			vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKm_ECU");
		//GPS里程
		}else if("5".equals(vehicleRumId)){
			vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_Tmileage");
			vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_Tmileage");
		//仪表里程(默认)
		} else {
			vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKmExact");
			vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKmExact");
		}
		double vclRumKmIntD = 0.0;
		double vclRumKmOutD = 0.0;
		if(vclRumKmInt.length()>0){
			vclRumKmIntD = Double.parseDouble(vclRumKmInt);
		}
		if(vclRumKmOut.length()>0){
			vclRumKmOutD = Double.parseDouble(vclRumKmOut);
		}
		if(ecmHour<=0){
			return averageSpeed;
		}else {
			averageSpeed = NumberFormat.round((vclRumKmIntD-vclRumKmOutD)/ecmHour, 1);
			return averageSpeed;
		}
		
	}
	
	/**
	 * 获取两个围栏间的平均载重
	 * @param fenMapInt
	 * @param fenMapOut
	 * @param VehicleID
	 * @return
	 */
	public double getMsgLSAvgLoad(Map<String,Object> fenMapInt,Map<String,Object> fenMapOut,String VehicleID){
		//开始围栏时间
		String beTime = MapGetter.getString(fenMapOut, "MsgAF_AlarmTime");
		//结束围栏时间
		String edTime = MapGetter.getString(fenMapInt, "MsgAF_AlarmTime");
		//比较两个时间的大小 
		if(DateRegUtil.getDaysOfTimeDef(new String[]{beTime,edTime})<0){
			return 0;
		}
		//开始时间（出围栏时间）小于区间开始时间的第一条数据(倒序)
		Map<String, Object> beMap = this.effectivenessStatDAO.queryBeLoad(VehicleID, beTime,edTime);
		//结束时间（进围栏时间）大于区间结束时间的第一条数据(正序)
		Map<String, Object> edMap = this.effectivenessStatDAO.queryEdLoad(VehicleID, edTime);
		
		double toLoad = 0.0;
		double averageLoad = 0.0;
		//获取时间段内的载重区间段
		List<Map<String, Object>> Listmap = this.effectivenessStatDAO.queryBwLoad(beMap, edMap,VehicleID);
		if(Listmap!=null && Listmap.size()>0){
			for(int i = 0;i<Listmap.size();i++){
				
				toLoad = NumberFormat.add(toLoad, NumberFormat.toDouble(Listmap.get(i).get("MsgESLS_AverageLoad").toString()));
			}
			//求平均并转换单位为吨
			//averageLoad = NumberFormat.round(NumberFormat.div((toLoad), Listmap.size()*1000),2);
			//求平均
			averageLoad = NumberFormat.round(NumberFormat.div((toLoad), Listmap.size()),2);
		}
		return averageLoad;
		
	}
	
	/**
	 * 获取两个围栏间的平均油耗
	 * @param fenMapInt
	 * @param fenMapOut
	 * @param VehicleID
	 * @return
	 */
	public double getMsgLSAvgOil(Map<String,Object> fenMapInt,Map<String,Object> fenMapOut,String vehicleRumId){
		
		double beOil = 0.0;
		double edOil = 0.0;
		double averageOil = 0.0;
		if(MapGetter.getString(fenMapOut, "MsgAF_tOilCons") == ""){
			beOil = 0.0;
		} else {
			beOil = Double.parseDouble(MapGetter.getString(fenMapOut, "MsgAF_tOilCons"));
		}
		if(MapGetter.getString(fenMapInt, "MsgAF_tOilCons") == ""){
			edOil = 0.0;
		} else {
			edOil = Double.parseDouble(MapGetter.getString(fenMapInt, "MsgAF_tOilCons"));
		}
		
		String vclRumKmOut = "";
		
		String vclRumKmInt = "";
		//仪表
		if("1".equals(vehicleRumId)){
					vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKmExact");
					vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKmExact");
		//ECM高精度
		} else if("2".equals(vehicleRumId)){
					vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKmExact_ECM");
					vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKmExact_ECM");
		//ECM总里程
		}else if("3".equals(vehicleRumId)){
					vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKm_ECM");
					vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKm_ECM");
		//ECU里程
		}else if("4".equals(vehicleRumId)){
					vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKm_ECU");
					vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKm_ECU");
		//GPS里程
		}else if("5".equals(vehicleRumId)){
					vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_Tmileage");
					vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_Tmileage");
		//仪表里程(默认)
		} else {
					vclRumKmOut = MapGetter.getString(fenMapOut, "MsgAF_tRunKmExact");
					vclRumKmInt = MapGetter.getString(fenMapInt, "MsgAF_tRunKmExact");
				}
		double vclRumKmIntD = 0.0;
		double vclRumKmOutD = 0.0;
		if(vclRumKmInt.length()>0){
			vclRumKmIntD = Double.parseDouble(vclRumKmInt);
		}
		if(vclRumKmOut.length()>0){
			vclRumKmOutD = Double.parseDouble(vclRumKmOut);
		}
		averageOil =NumberFormat.round(NumberFormat.div((edOil-beOil)*100, (vclRumKmIntD-vclRumKmOutD)),1);
		return averageOil;
		
	}
	public static void main(String[] args) {
		System.out.println(DateRegUtil.getDaysOfTimeDef(new String[]{"2015-01-10","2015-01-05"}));
		
	}
}
